package com.loongtech.bi.action.bigame;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.game.EntityActiveUserSchool;
import com.loongtech.bi.entity.game.EntityNewUserSchool;
import com.loongtech.bi.manager.game.ActiveUserSchoolManager;
import com.loongtech.bi.manager.game.NewUserSchoolManager;
import com.loongtech.bi.manager.game.SchoolGameManager;
import com.loongtech.bi.manager.game.SchoolGameManager.SchoolGameData;
import com.loongtech.bi.manager.game.SchoolGameManager.SchoolGameSumData;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 游戏内分析-职业分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/game/schoolGame")
public class SchoolGameBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 8 * Time.DAY));  // 前端传过来的开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的结束时间
	
	private List<SchoolGameSumData> schoolGameSumData;
	private List<ChartUIData> pieChartList;  // 前端要显示的二个饼状图数据
	private List<SchoolGameData> schoolGameData;
	
	@Autowired
	SchoolGameManager schoolGameManager;
	@Autowired
	ActiveUserSchoolManager activeUserSchoolManager;
	@Autowired
	NewUserSchoolManager newUserSchoolManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            pieChartList = new ArrayList<>();
            schoolGameSumData = new ArrayList<>();
            schoolGameData = new ArrayList<>();

            List<EntityActiveUserSchool> activeUserSchoolList = activeUserSchoolManager.getAllActiveUserSchool(dateStart, dateEnd);
            List<EntityNewUserSchool> newUserSchoolList = newUserSchoolManager.getAllNewUserSchool(dateStart, dateEnd);
            if ((activeUserSchoolList != null && activeUserSchoolList.size() > 0) || (newUserSchoolList != null && newUserSchoolList.size() > 0)) {
                schoolGameManager.updateSchoolData(dateStart, dateEnd, activeUserSchoolList, newUserSchoolList, schoolGameSumData, pieChartList, schoolGameData);
            } else {
                if (activeUserSchoolList == null || activeUserSchoolList.size() <= 0) {
                    schoolGameSumData = null;
                }
                if (newUserSchoolList == null || newUserSchoolList.size() <= 0) {
                    schoolGameData = null;
                }
            }

            // 转职业ID为职业名
            for (SchoolGameSumData schoolGameSumDatum : schoolGameSumData) {
                Map<String, Integer> ss = schoolGameSumDatum.getSchoolCounts();
                Map<String, Integer> ssnew = new LinkedHashMap<>();

                for (Map.Entry<String, Integer> entry : ss.entrySet()) {
                    ssnew.put(getSchoolNameById(entry.getKey()), entry.getValue());
                }
                schoolGameSumDatum.setSchoolCounts(ssnew);
            }
            for (SchoolGameData schoolGameDatum : schoolGameData) {
                Map<String, Integer> ss1 = schoolGameDatum.getActiveUserSchoolCounts();
                Map<String, Integer> ss2 = schoolGameDatum.getNewUserSchoolCounts();
                Map<String, Integer> ss1new = new LinkedHashMap<>();
                Map<String, Integer> ss2new = new LinkedHashMap<>();

                for (Map.Entry<String, Integer> entry : ss1.entrySet()) {
                    ss1new.put(getSchoolNameById(entry.getKey()), entry.getValue());
                }
                schoolGameDatum.setActiveUserSchoolCounts(ss1new);
                for (Map.Entry<String, Integer> entry : ss2.entrySet()) {
                    ss2new.put(getSchoolNameById(entry.getKey()), entry.getValue());
                }
                schoolGameDatum.setNewUserSchoolCounts(ss2new);
            }

            result.put("res", "0");
            result.put("schoolGameSumData", schoolGameSumData);
            result.put("pieChartList", pieChartList);
            result.put("schoolGameData", schoolGameData);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	public String getSchoolNameById(int id) {
		return GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(String.valueOf(id)); //SchoolManager.getSchoolNameById(id);
	}

    public String getSchoolNameById(String id) {
        return GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(id); //SchoolManager.getSchoolNameById(id);
    }

    @Override
    public void search() {
    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

	public List<ChartUIData> getPieChartList() {
		return pieChartList;
	}

	public void setPieChartList(List<ChartUIData> pieChartList) {
		this.pieChartList = pieChartList;
	}

	public List<SchoolGameData> getSchoolGameData() {
		return schoolGameData;
	}

	public void setSchoolGameData(List<SchoolGameData> schoolGameData) {
		this.schoolGameData = schoolGameData;
	}

	public List<SchoolGameSumData> getSchoolGameSumData() {
		return schoolGameSumData;
	}

	public void setSchoolGameSumData(List<SchoolGameSumData> schoolGameSumData) {
		this.schoolGameSumData = schoolGameSumData;
	}
}
